const app = getApp();
const util = require('../../utils/util.js');
Page({
    data: {
        used: {},
        isClick: true,
        isClick_: true,
        payClick: true,
        checkNum: 0,
        used_id: "",
        order_id: "",
        sn: "",
        timer: '',
    },
    onLoad(options) {
        my.showLoading({
            content: 'loading...'
        });
        let usedId = options.used_id;
        this.getUsedInfo(usedId);
        my.hideLoading();
    },
    // 页面隐藏/切入后台时触发。 如 my.navigateTo 到其他页面或底部 tab 切换等
    onHide() {
        this.setData({
            checkNum: 0,
            isClick: true,
            payClick: true
        });
        clearInterval(this.data.timer);
    },
    // 页面卸载时触发。 如 my.redirectTo 或 my.navigateBack 到其他页面等。
    onUnload() {
        this.setData({
            checkNum: 0,
            isClick: true,
            payClick: true
        });
        clearInterval(this.data.timer);
    },
    handleCopy(e) {
        let usedSn = e.currentTarget.dataset.usedSn;
        this.setData({
            usedSn: usedSn
        });
        console.log(usedSn);
        my.setClipboard({
            text: this.data.usedSn,
        });
        my.showToast({
            content: '复制成功',
            type: 'none'
        });
    },

    // ============================ 获取使用记录信息 ========================
    getUsedInfo(usedId) {
        let that = this;
        // 获取该使用记录的详细信息
        util.httpRequest('/getUsedInfo', {
            used_id: usedId
        }, function (res) {
            if (res.code == 1) {
                that.setData({
                    used: res.data,
                    sn: res.data.rentDeviceSn
                })
            }
        }, function () {
            that.setData({
                isClick_: true
            })
        })
    },


    // ======================= 未支付订单调起支付 ===========================
    toPay(e) {
        let usedId = e.currentTarget.dataset.id;
        this.setData({
            used_id: usedId
        });
        this.goToPay();
    },
    // ======================= 发起扣款 =================
    goToPay() {
        let that = this;
        let used_id = that.data.used_id;
        let order_id = that.data.order_id;
        if (that.data.payClick) {
            that.setData({
                payClick: false
            });
            my.showLoading({
                content: 'loading...'
            });
            // 第一步先校验是否是需要支付的预授权订单
            util.httpRequest('/doToPay', {
                used_id: used_id,
                order_id: order_id
            }, function (res) {
                my.hideLoading();
                switch (res.code) {
                    case 1:
                        // 支付成功，重新加载数据
                        my.hideLoading();
                        my.showToast({
                            content: res.msg,
                            type: 'none'
                        });
                        // 重新加载
                        that.getUsedInfo(res.data.used_id);
                        break;
                    case 2:
                        // 未支付。需要发起支付
                        my.showLoading({
                            content: '请稍后...'
                        });
                        util.alipayPayment(res.data.amount, res.data.type, res.data.sn, res.data.used_id, function (order_id) {
                            my.hideLoading();
                            if (order_id != null) {
                                that.checkOrderStatus(order_id);
                            } else {
                                that.setData({
                                    payClick: false
                                });
                            }
                        }, function () {
                            that.setData({
                                payClick: true
                            });
                            my.hideLoading();
                        });
                        break;
                    default:
                        that.setData({
                            payClick: true
                        })
                        my.hideLoading();
                        break;
                }
            }, function () {
                that.setData({
                    payClick: true
                })
            })
        }
    },

    // ====================== 校验用户是否支付成功  ===========================================
    checkOrderStatus(order_id) {
        let that = this;
        let check = that.data.checkNum;
        check++;
        that.setData({
            checkNum: check
        });
        if (that.data.checkNum < 20) {
            util.httpRequest('/checkOrderStatus', {
                order_id: order_id,
                freeDeposit: 0
            }, function (res) {
                if (res.data.status == 1) {
                    my.hideLoading();
                    that.setData({
                        order_id: res.data.order_id,
                        payClick: true
                    });
                    clearInterval(that.data.timer);
                    that.goToPay();
                } else {
                    var timerTem = setTimeout(() => {
                        // 定时器的执行方法
                        that.checkOrderStatus(order_id);
                    }, 2000);
                    that.setData({
                        timer: timerTem
                    });
                }
            }, function () {
                my.hideLoading();
            })
        } else {
            my.showToast({
                content: '订单异常,请联系客服',
                duration: 2000
            });
            my.hideLoading();
        }
    },

    toBorrow() {
        my.navigateTo({
            url: '/pages/index/index'
        })
    }
});
